#ifndef __MY_LOG_H__
#define __MY_LOG_H__

#include "defines.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"

#define MLOG_DEBUG	spdlog::debug
#define MLOG_INFO	spdlog::info
#define MLOG_WARN	spdlog::warn
#define MLOG_ERROR	spdlog::critical

class MLog {
public:
    MLog() {
        init(spdlog::level::debug, spdlog::level::debug);
    }

    virtual ~MLog() {
        // MLOG_DEBUG("~MLog");
    }

	void init(spdlog::level::level_enum fileLevel, spdlog::level::level_enum consoleLevel) {
		const char pattern[] = "[%^%L%$] [%Y-%m-%d %H:%M:%S.%e] [pid %P] [tid %t] %v";
		auto max_files = 5;
		auto max_size = 1024 * 1024;
		auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("./logs/main.log", max_size, max_files);
		file_sink->set_level(fileLevel);
		file_sink->set_pattern(pattern);

		auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
		console_sink->set_level(consoleLevel);
		console_sink->set_pattern(pattern);

		spdlog::logger logger("main", { console_sink, file_sink });
		spdlog::set_default_logger(std::make_shared<spdlog::logger>(logger));
		spdlog::set_level(consoleLevel);
		spdlog::flush_on(fileLevel);
		MLOG_DEBUG("init MLog");
	}
};

static MLog sMLog;

#endif // __MY_LOG_H__